package org.teach.study.lambda;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.teach.study.lambda.vo.Dept;
import org.teach.study.lambda.vo.Emp;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		testCollector();
	}

	private static void testCollector() {
		Collection<Emp> list = createEmpList();

		long count = list.stream().collect(Collectors.counting());
		System.out.println("总条数:" + count);
		Optional<Emp> v = list.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Emp::getSalary)));
		System.out.println("工资最高的人:" + v.get().getName() + "," + v.get().getSalary());

		Function<Emp, Dept> f = Emp::getDept;
		Map<Dept, List<Emp>> g = list.stream().collect(Collectors.groupingBy(f));
		g.forEach((key, value) -> System.out
				.println(key.getName() + "工资小计：" + value.stream().mapToDouble(Emp::getSalary).sum()));
	}

	private static void testStream() {
		Collection<Emp> list = createEmpList();
		long count = list.size();
		System.out.println("总条数:" + count);
		count = list.stream().filter(emp -> emp.id() % 2 == 1).count();
		System.out.println("奇数条数:" + count);

		list.stream().limit(2).forEach((emp) -> System.out.println(emp.getName()));

		Optional<Emp> v = list.stream().findFirst();
		System.out.println("first:" + v.get().getName());

		list.stream().map(Emp::getName).forEach(name -> System.out.print(name + ","));
		System.out.println("工资总计:" + list.stream().mapToDouble(Emp::getSalary).sum());
	}

	private static void testOptional() {
		Optional<String> v = Optional.of("hello");
		System.out.println("v=" + v.map(t -> "hi".equals(t) ? "嘿" : "哈喽").get());

		System.out.println(v.orElse("hi"));
		v.ifPresent((t) -> System.out.println("存在：" + t));
		v = v.filter((t) -> {
			return "hi".equals(t);
		});
		System.out.println("存在：" + v.isPresent());

		System.out.println(v.orElseGet(() -> "耶"));
		v.ifPresent((t) -> System.out.println("存在：" + t));
	}

	/**
	 * <b>创建员工名单。</b>
	 * <p><b>详细说明：</b></p>
	 * <!-- 在此添加详细说明 -->
	 * 无。
	 * @return 员工名单
	 */
	private static List<Emp> createEmpList() {
		Dept dept1 = new Dept(1L, "人事部");
		Dept dept2 = new Dept(2L, "技术部");
		Dept dept3 = new Dept(3L, "销售部");
		List<Emp> empList = new ArrayList<>();
		empList.add(new Emp(1L, "小赵", Date.valueOf("1991-01-01"), 10000, 2, dept1));
		empList.add(new Emp(2L, "小钱", Date.valueOf("1991-02-01"), 11000, 2, dept1));
		empList.add(new Emp(3L, "小孙", Date.valueOf("1991-03-01"), 15000, 1, dept2));
		empList.add(new Emp(4L, "小李", Date.valueOf("1991-04-01"), 13000, 1, dept2));
		empList.add(new Emp(5L, "小周", Date.valueOf("1991-05-01"), 11000, 2, dept2));
		empList.add(new Emp(6L, "小武", Date.valueOf("1991-06-01"), 12000, 1, dept3));
		empList.add(new Emp(7L, "小郑", Date.valueOf("1991-07-01"), 17000, 2, dept3));
		empList.add(new Emp(8L, "小王", Date.valueOf("1991-08-01"), 18000, 2, dept3));

		return empList;
	}
}
